<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜数字</title>
</head>
<body>
    <h3>请输入要猜的数字: </h3>
    <input type="text">
    <div>
        已经猜的次数: <span id="guessCount">0</span>
    </div>
    <div>
        结果是: <span id="result"></span>
    </div>
    <button>猜</button>

    
    <script>
        // 0. 获取到需要的元素
        let input = document.querySelector('input');
        let button = document.querySelector('button');
        let guessCount = document.querySelector('#guessCount');
        let result = document.querySelector('#result');

        // 1. 在页面加载的时候, 要生成 1 - 100 之间随机的整数
        let toGuess = parseInt(Math.random() * 100) + 1;
        console.log(toGuess);

        // 2. 给 猜 这个按钮加上个点击事件. 
        button.onclick = function() {
            // a) 获取到输入框里的值. 
            let value = parseInt(input.value);
            // b) 和当前的生成的数字进行对比. 
            if (value < toGuess) {
                result.innerHTML = '低了';
            } else if (value > toGuess) {
                result.innerHTML = '高了';
            } else {
                result.innerHTML = '猜对了!';
            }
            // c) 每点击一次提交, 就让 猜 的次数, 加1!
            let guessCountValue = parseInt(guessCount.innerHTML);
            guessCount.innerHTML = guessCountValue + 1;
        }
    </script>
</body>
</html>